<?php

namespace Biz\model\salesRebate;

use Doraemon\pockets\datebase\BaseDbModel;
use Biz\module\Classes\CouponRebate as cCouponRebate;
use Doraemon\tools\ImgUrl as toolImgUrl;
use Biz\cache\salesrebate\Coupon as cCoupon;
use Kafka\Exception;
use Biz\model\salesRebate\SalesCouponSingle as mSalesCouponSingle;

class SalesCoupon extends BaseDbModel
{
    const _CONFIG_FILE_ = 'db_shop';

    const _CONFIG_SELECT_ = 'db_sales_rebate';

    const _DATABASE_ = 'biz_sales_rebate';

    const _TABLE_NAME = 'sales_coupon';

    //状态码
    const STATUS_NORMAL = 0;// 未审核
    const STATUS_OPEN = 1;//开启，可以使用
    const STATUS_DELETE = 9;//优惠码删除


    public static function getAllByWhere($files = '*', $where = [])
    {
        $data = self::select($files, $where);
        $data = self::formatInfo($data);
        return $data;
    }

    public static function getOneByWhere($files = '*', $where = [])
    {
        $data = self::get($files, $where);
        $data = self::formatInfo([$data]);
        return $data[0];
    }

    public static function formatInfo($info)
    {
        if (empty($info)) {
            return;
        }
        $data = array_map(function($_item){
            if(isset($_item['cover_img']) && $_item['cover_img']<>'') {
                $_item['cover_img_url'] = toolImgUrl::getUrl(toolImgUrl::APPTYPE_OP, $_item['cover_img'], toolImgUrl::IMAGE_SIZE_OP_ORI);
            }
            return $_item;
        },$info);
        return $data;
    }

    public static function create($params)
    {
        $result['status'] = true;

        //新建
        $getOne = self::getOneByWhere('*',['uid'=>$params['uid']]);
        if($getOne){
            if($getOne['status']!=self::STATUS_DELETE) {
                $result['status'] = false;
                $result['msg'] = '用户已经加入白名单';
                return $result;
            }else{
                $update = self::updatebyWhere(['status'=>self::STATUS_NORMAL],['uid'=>$params['uid']]);
                if(!$update){
                    $result['status'] = false;
                    $result['msg'] = '修改失败';
                }
                return $result;
            }
        }

        $data = cCouponRebate::createSalesRebateCoupon($params['uid'],$params);
        if(!$data || empty($data)){
            $result['status'] = false;
            $result['msg'] = '创建失败';
        }else{
            //存入缓存
            $data['status'] = 0;
            self::saveCache($params['uid'], $data);
        }
        return $result;
    }

    public static function saveCache($key, $data){
        foreach($data as $_k=>&$_v){
            cCoupon::setHash($key, $_k, $_v);
        }
    }

    public static function getCache($uids){
        $result = [];
//        $result = cCoupon::getAllHash($uids);
//        if(empty($result)){
            $result = self::getAllByWhere('*', ['uid'=>$uids]) ;
//        }
       return $result;
    }

    public static function updatebyWhere($data, $where){
        if(!isset($where['uid'])) {
            $getOne = self::getOneByWhere('*', ['id' => $where['id']]);
        }else{
            $getOne = self::getOneByWhere('*', ['uid' => $where['uid']]);
        }

        $result = true;
        try{
            if(isset($data['status'])) {
                $coupon_id = '';
                $coupon_id = mSalesCouponSingle::getAllByWhere('coupon_id',['uid'=>$getOne['uid']]);
                if ($data['status'] == self::STATUS_DELETE) {
                    cCouponRebate::disableSalesRebateCoupon($getOne['person_coupon_id'], $getOne['company_coupon_id'], $coupon_id);
//                } else if ($getOne['status'] == self::STATUS_DELETE && $data['status'] != self::STATUS_DELETE) {
//                    cCouponRebate::enableSalesRebateCoupon($getOne['person_coupon_id'], $getOne['company_coupon_id'], $coupon_id);
                }else if($data['status'] == self::STATUS_OPEN){
                    cCouponRebate::enableSalesRebateCoupon($getOne['person_coupon_id'], $getOne['company_coupon_id'], $coupon_id);
                }
            }
            self::update(
                $data,
                $where
            );
            self::saveCache($getOne['uid'],$data);
        }catch (Exception $e){
            $result = false;
        }
        return $result;
    }

    public static function getInfo($uid)
    {
        return self::get('*', ['uid' => $uid]);
    }

    public static function createOne($uid, $tcCoupon=[], $tpCoupon=[], $params = [])
    {
        $data = [
            'uid' => $uid,
            'belong_area' => $params['belongs'],
            'person_coupon_code' => $tpCoupon['code'],
            'person_coupon_id' => $tpCoupon['id'],
            'company_coupon_code' => $tcCoupon['code'],
            'company_coupon_id' => $tcCoupon['id'],
            'admin_id'=> isset($params['admin_id'])?$params['admin_id']:0
        ];

        try{
            self::insert($data);
            return $data;
        }catch(Exception $e){
            return false;
        }


    }

}